//
// Created by tcj on 2022/3/21.
//

#ifndef __AYLAR_ENDIAN_H__
#define __AYLAR_ENDIAN_H__

#define SYLAR_LITTLE_ENDIAN 1
#define SYLAR_BIG_ENDIAN 2

#include "byteswap.h"
#include "stdint.h"

namespace sylar{
    template<class T>
    typename std::enable_if<sizeof(T) == sizeof(uint64_t),T>::type
    byteswap(T value){
        return (T) bswap_64((uint64_t)value);
    }

    template<class T>
    typename std::enable_if<sizeof(T) == sizeof(uint32_t),T>::type
    byteswap(T value){
        return (T) bswap_32((uint32_t)value);
    }

    template<class T>
    typename std::enable_if<sizeof(T) == sizeof(uint16_t),T>::type
    byteswap(T value){
        return (T) bswap_16((uint16_t)value);
    }

    #if BYTE_ORDER == BIG_ENDIAN
    #define SYLAR_BYTE_ORDER SYLAR_BIG_ENDIAN
    #else
    #define SYLAR_BYTE_ORDER SYLAR_LITTLE_ENDIAN
    #endif

    #if SYLAR_BYTE_ORDER == SYLAR_BIG_ENDIAN
    template<class T>
    T byteswapOnlittleEndian(T t){
        return t;
    }

    template<class T>
    T byteswapOnBigEndian(T t){
        return byteswap(t);
    }
    #else
    template<class T>
    T byteswapOnlittleEndian(T t){
        return byteswap(t);
    }

    template<class T>
    T byteswapOnBigEndian(T t){
        return t;
    }
    #endif

};

#endif //SERVER_FRAMEWORK_ENDIAN_H
